define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue','template','fileinput'], function ($, undefined, Backend, Table, Form,Vue,Template,fileinput) {
    //初始化fileinput控件（第一次初始化）
    var file_path = '';
    function initFileInput(ctrlName, uploadUrl) {
        $('#upload_file').fileinput('refresh',{
            language: 'zh', //设置语言
            uploadUrl: uploadUrl, //上传的地址
            allowedFileExtensions : ['xls','xlsx'],//接收的文件后缀
            showUpload: true, //是否显示上传按钮
            showRemove:false,
            showCaption: false,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            initialPreviewConfig:[],
            previewFileIcon: "<i  class='glyphicon glyphicon-king'></i>",
            showPreview: true, //是否显示预览
        }).on("fileuploaded", function(event, data) {
            console.info(ctrlName);
            console.info(data);
            if(data.response.code == 1){
                Toastr.success(data.response.msg);
                $('#'+ctrlName).val(data.response.data.url);
                file_path = data.response.data.url;
            }else{
                Toastr.error(data.response.msg);
            }
        }).on("filesuccessremove", function (event, data, previewId, index) {
            if(file_path !=''){
                $.post("adminpublic/ajax_delete_file", {
                    file_path : file_path
                }, function (res) {
                    if(data.response.code == 1){
                        Toastr.success(data.response.msg);
                    }else{
                        Toastr.error(data.response.msg);
                    }
                });
            }
            return true;
        });
    };
    //vue
    function initSaveVue() {
        var vue = new Vue({
            el:'#index_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                province_id : '',
                city_id : '',
                county_id : '',
                name : '',
                address : '',
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                }
            }
        });
        return vue;
    }

    function initSearchVue() {
        var province_vue = new Vue({
            el:"#province_div",
            data:{
                province_list: [],
            },
            watch: {
            },methods:{
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                }
            }
        });
        var city_vue = new Vue({
            el:"#city_div",
            data:{
                city_list: [],
                county: ''
            },
            watch: {
            }
        });
        var county_vue = new Vue({
            el:"#county_div",
            data:{
                county_list: [],
                city: ''
            },
            watch: {
            }
        });
        function getCityList(parent_id) {
            var data = {
                parent_id : parent_id,
            };
            $.post("adminpublic/ajax_get_region_list",data,function(result) {
                city_vue.city_list = result;
            });
        }
        function getCountyList(parent_id) {
            var data = {
                parent_id : parent_id,
            };
            $.post("adminpublic/ajax_get_region_list",data,function(result) {
                county_vue.county_list = result;
            });
        }
        province_vue.getProvinceList();
        $('select[name=province]').on('change', function () {
            getCityList(this.value);
        });
        $('select[name=city]').on('change', function () {
            getCountyList(this.value);
        });
    }
    var Controller = {
        index: function () {
            var user_id = Config.user_id;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'village/index/ids/'+user_id,
                    add_url: 'village/add/ids/'+user_id,
                    edit_url: 'village/edit',
                    del_url: 'village/del',
                    send_message_url:'village/send_message',
                    multi_url: 'village/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'address', title: __('Address'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {field: 'province_name', title: __('Province'),searchable:false},
                        {field: 'city_name', title: __('City'),searchable:false},
                        {field: 'county_name', title: __('County'),searchable:false},
                        {field: 'province', title: __('Province'),visible:false,searchList: function (column) {
                            return Template('province_search', {});
                        }
                        },
                        {field: 'city', title: __('City'),visible:false,searchList: function (column) {
                            return Template('city_search', {});
                        }
                        },
                        {field: 'county', title: __('County'),visible:false,searchList: function (column) {
                            return Template('county_search', {});
                        }
                        },
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {name: 'send', text: '发送', title: '发送', icon: 'fa fa-list', classname: 'btn btn-xs btn-primary btn-dialog', url: 'village/send_message'}
                            ]
                            ,events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            initSearchVue();
            /*$(document).on("click", ".btn-import", function () {
                var selectdList = table.bootstrapTable('getSelections');
                console.info(selectdList.length);
                if(selectdList.length == 0){
                    window.location.href = Config.site.cdnurl+"/admin.php/Village/import?dialog=1";
                }
                else{
                    window.location.href = Config.site.cdnurl+"/admin.php/Village/import?id="+selectdList[0]['id'];
                }
            });*/
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initSaveVue();
            vue.getProvinceList();
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initSaveVue();
            vue.getProvinceList();
            var village = Config.village;
            vue.province_id = village.province_id;
            vue.city_id = village.city_id;
            vue.county_id = village.county_id;
            vue.name = village.name;
            vue.address = village.address;
        },
        import:function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = new Vue({
                el:'#index_div',
                data:{
                    province_list: [],
                    city_list: [],
                    county_list: [],
                    village_list: [],
                    province_id : 0,
                    city_id : 0,
                    county_id : 0,
                    village_id : 0
                },
                watch:{
                    province_id:function (val) {
                        this.province_id = val;
                        this.getCityList();
                        this.village_list = [];
                        this.city_list = [];
                        this.county_list = [];
                    },
                    city_id:function (val) {
                        this.city_id = val;
                        this.getCountyList();
                        this.village_list = [];
                        this.county_list = [];
                    },
                    county_id:function (val) {
                        this.county_id = val;
                        this.getVillageList();
                    }
                },
                methods: {
                    getProvinceList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : 0
                        }, function (res) {
                            that.province_list = res;
                        });
                    },
                    getCityList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : that.province_id
                        }, function (res) {
                            that.city_list = res;
                        });
                    },
                    getCountyList:function(){
                        var that = this;
                        $.post("adminpublic/ajax_get_region_list", {
                            parent_id : that.city_id
                        }, function (res) {
                            that.county_list = res;
                        });
                    },
                    getVillageList:function(){
                        var that = this;
                        $.get("adminpublic/ajax_village_list", {
                            county_id : that.county_id
                        }, function (res) {
                            that.village_list = res;
                        });
                    }
                }
            });
            vue.getProvinceList();
            var url = Config.site.cdnurl+'/index.php/admin/adminpublic/upload';
            var controlName = 'upload_file_path';
            initFileInput(controlName,url);
        },
        send_message:function () {
            Form.api.bindevent($("form[role=form]"));
        }
    };
    return Controller;
});